package test;

/**
 * Created by hdt3213 on 17/4/3.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.HashMap;

import org.json.JSONObject;

public class ChatClient extends Thread {
	public String register() {
		HashMap<String, String> request = new HashMap<String, String>();

		request.put("action", "account/register");
		request.put("name", "a");
		request.put("password", "1234");
		request.put("face", "1");
		JSONObject json = new JSONObject(request);

		return json.toString();
	}

	public String login() {
		HashMap<String, String> request = new HashMap<String, String>();

		request.put("action", "account/login");
		request.put("uid", "33299650338");
		request.put("password", "1234");
		JSONObject json = new JSONObject(request);

		return json.toString();
	}

	public void listen(Socket socket) throws IOException {
		BufferedReader buf =  new BufferedReader(new InputStreamReader(socket.getInputStream()));
		String echo;
		while(true) {
			echo = buf.readLine();
			if (echo != null) {
				System.out.println(echo);
			}
		}
	}

	public void run(){

		try {
			Socket client = new Socket("127.0.0.1", 5000);
			client.setSoTimeout(1000000);


			PrintStream out = new PrintStream(client.getOutputStream());

			out.println(register());

			listen(client);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		ChatClient client = new ChatClient();
		client.start();
	}

}
